<template>
  <el-form ref="deptForm" :rules="relesof" label-width="120px" :model="form">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select></el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addpartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    curId: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originlist: {
      type: Array,
      required: true
    }
  },
  data() {
    // 自定义验证规则
    const valicode = (rule, value, callback) => {
      let exit = this.originlist.map(item => item.code)
      if (this.isEdit) {
        exit = this.originlist.filter(item => item.id !== this.curId).map(item => item.code)
      }
      if (exit.includes(value)) {
        callback(new Error('部门编码' + value + '已存在'))
      } else {
        callback()
      }
    }
    // z名字规则
    const valiname = (rule, value, callback) => {
      let exit = this.originlist.filter(item => item.pid === this.curId).map(item => item.name)
      if (this.isEdit) {
        const ad = this.originlist.find(item => item.id === this.curId)
        const pid = ad.pid
        exit = this.originlist.filter(item => item.pid === pid && item.id !== this.curId).map(item => item.name)
      }
      exit.includes(value) ? callback(new Error('部门名称' + value + '已存在')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      list: [],
      relesof: {
        name:
         [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
           { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
           { validator: valiname, trigger: 'blur' }
         ],
        code:
         [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
           { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
           { validator: valicode, trigger: 'blur' }
         ],
        manager:
         [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        ntroduce:
         [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
           { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadEmployeeSimple()
    if (this.isEdit) {
      this.ADepartDetail()
    }
  },
  methods: {
    // 员工列表
    async loadEmployeeSimple() {
      try {
        const res = await getEmployeeSimple()
        this.list = res.data
        console.log(res, '员工信息')
      } catch (error) {
        console.log(error)
      }
    },
    // 确定
    hSubmit() {
      this.$refs.deptForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.lodateDepartments() : this.loadaddpartments()
      })

      this.$emit('success')
    },
    // 取消
    hCancel() {
      this.$emit('close')
    },
    // 新增函数
    async loadaddpartments() {
      try {
        await addpartments({ pid: this.curId, ...this.form })
        this.form = {}
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑函数
    async ADepartDetail() {
      try {
        const res = await getDepartDetail(this.curId)
        console.log(res, '编辑')
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 修改ajax
    async lodateDepartments() {
      try {
        await updateDepartments(this.form)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
